package com.example.easypad;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

public class activity_login extends AppCompatActivity {
    private EditText et_useName;
    private EditText et_passWord;
    private Button btn_login;
    private TextView tv_gotoRegister;
    String userName,passWord,uid,sql = "";
    public static SQLiteOpenHelper helper = null;
    public static SQLiteDatabase db;
    private Cursor cursor = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        MakeStatusBarTransparent.makeStatusBarTransparent(this);
        initLayout();
        helper = new ContactSQLiteOpenHelper(this,"demo.db",null,2);
        db = helper.getWritableDatabase();
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ((et_useName.getText() + "").equals("") || (et_passWord.getText() + "").equals("")) {
                    Toast.makeText(getApplicationContext(), "请完善登录信息", Toast.LENGTH_SHORT).show();
                    return;
                }
                queryAllUser(view);
            }
        });

        tv_gotoRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), activity_register.class);
                startActivity(intent);
            }
        });

    }

    public void queryAllUser(View view) {
        sql = "select * from User";
        queryUser(sql,null);
    }

    @SuppressLint("Range")
    private void queryUser(String sql,String[] selection) {
        cursor = db.rawQuery(sql, selection);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            uid = cursor.getString(cursor.getColumnIndex("uid"));
            userName = cursor.getString(cursor.getColumnIndex("userName"));
            passWord = cursor.getString(cursor.getColumnIndex("passWord"));
            Log.d("queryUser", "用户名：" + userName + "，密码：" + passWord);
            cursor.moveToNext();
            if ((et_useName.getText() + "").equals(userName) && (et_passWord.getText() + "").equals(passWord)) {
                Intent intent = new Intent(activity_login.this,MainActivity.class);
                intent.putExtra("uid",uid);
                intent.putExtra("userName",userName);
                intent.putExtra("passWord",passWord);
                startActivity(intent);
                return;
            }
        }
        Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
    }
    private void initLayout() {
        et_useName = findViewById(R.id.et_useName);
        et_passWord = findViewById(R.id.et_passWord);
        btn_login = findViewById(R.id.btn_login);
        tv_gotoRegister = findViewById(R.id.tv_gotoRegister);
    }

}